import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JSeparator;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JList;
import java.util.List;
import java.util.Set;
import java.util.ArrayList;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;

public class VueSaisieParution extends Vue {

	private JFrame frame;
	private DefaultListModel modelPeriodique = new DefaultListModel();
	private JTextField textFieldDate;
	private JTextField textFieldTitre;
	private JTextField textFieldIdParution;
	private JList list;
	private JButton btnQuitter;
	private JButton btnValider;

	/**
	 * Create the application.
	 */
	public VueSaisieParution(Controleur controleur) {
		super(controleur);
		this.initialize(null,null);
	}

	public VueSaisieParution(Controleur controleur, String issn, String NomPeriodique) {

		super(controleur);
		this.initialize(issn,NomPeriodique);

	}
	
	
	public JFrame getFrame() {
		return frame;	
	}
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize(String issn,String nom) {
		frame = new JFrame();
		frame.setBounds(100, 100, 527, 255);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		btnQuitter = new JButton("Quitter");
		btnQuitter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setVisible(false);
				getControleur().fermerVue(VueSaisieParution.this);
			}
		});

		btnValider = new JButton("Valider");
		btnValider.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String idParution = textFieldIdParution.getText();
				String titre = textFieldTitre.getText();
				String date = textFieldDate.getText();
				Object selection = list.getSelectedValue();
				GregorianCalendar dateMod = ESDate.lireDate(date);

				if (idParution.length() == 0 || titre.length() == 0 || date.length()==0)
				{
					Message dialog = new Message("Tous les champs doivent etre remplis.");
					dialog.setVisible(true);
				}
				else if(selection == null){
					Message dialog = new Message("Vous devez selectionner le périodique associé à la nouvelle parution.");
					dialog.setVisible(true);
				}
				else if (dateMod == null) {
					Message dialog = new Message("Le format de la date est incorrect");
					dialog.setVisible(true);
				}
				else{
					boolean dejaEnr;
					String selectionChaine = selection.toString();
					String issn = selectionChaine.split(" ")[0];
					Periodique per = getControleur().getPeriodique(issn);
					dejaEnr = getControleur().ajouterParution(per,idParution, titre, date);
					if (! dejaEnr){
						getControleur().AjouterArticle(idParution);
					frame.setVisible(false);
					}
				}
			}
		});
		list = new JList(modelPeriodique);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		alimentePeriodiques(issn,nom);
		
		textFieldDate = new JTextField();
		textFieldDate.setColumns(10);
		
		textFieldTitre = new JTextField();
		textFieldTitre.setColumns(10);
		
		textFieldIdParution = new JTextField();
		textFieldIdParution.setColumns(10);
		
		JLabel label = new JLabel("IdParution :");
		
		JLabel label_1 = new JLabel("Titre :");
		
		JLabel label_2 = new JLabel("Date parution:");
		
		JLabel lblParutionDuPriodique = new JLabel("Parution du périodique :");
		
		JLabel label_4 = new JLabel("Informations générales de la parution :");
		label_4.setFont(new Font("Dialog", Font.BOLD, 12));
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(136)
							.addComponent(label_4, GroupLayout.PREFERRED_SIZE, 243, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(list, GroupLayout.PREFERRED_SIZE, 287, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(label, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
								.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
								.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE))
							.addGap(12)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(textFieldIdParution, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
								.addComponent(textFieldTitre, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
								.addComponent(textFieldDate, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(89)
							.addComponent(btnQuitter, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE)
							.addGap(146)
							.addComponent(btnValider, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE))
						.addComponent(lblParutionDuPriodique, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(label_4, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblParutionDuPriodique)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(1)
							.addComponent(list, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(label)
							.addGap(26)
							.addComponent(label_1)
							.addGap(25)
							.addComponent(label_2))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(textFieldIdParution, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(20)
							.addComponent(textFieldTitre, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(21)
							.addComponent(textFieldDate, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(20)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(btnQuitter)
						.addComponent(btnValider))
					.addContainerGap(448, Short.MAX_VALUE))
		);
		
		frame.getContentPane().setLayout(groupLayout);
		frame.setVisible(true);
	}
	
	public void setEtat (int etat){
		switch (etat) {
		case initiale: {
			btnValider.setEnabled(true);
			btnQuitter.setEnabled(true);
			break;
			}
		}
	}
	
	
	
	public void alimentePeriodiques(String issn, String nom) {
		//HashMap<String, Periodique>  periosTab = getControleur().get_periodiques();
		HashSet<Periodique> perioSet = getControleur().getPeriodiqueSet();
		if (issn==null && nom == null)
		{
		if (perioSet!= null){
			for (Periodique per : perioSet) {
				modelPeriodique.addElement(per.get_issn() + "                 " + per.get_NomPeriodique());

			}
		}
		else
		{
			Message dialog = new Message("Pas de périodiques.");
			dialog.setVisible(true);
		}
		}
		else
			modelPeriodique.addElement(issn + "                 " + nom);
	}
	public void fermerVue(Vue vue) {
		if (vue instanceof VueSaisieParution) {
			// Quitte l'aplication.
			System.exit(0);
		}
	}
}